﻿#pragma execution_character_set("utf-8")
#include "warehousingwidget.h"
#include "ui_warehousingwidget.h"
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include "database.h"
#include <QApplication>

WarehousingWidget::WarehousingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WarehousingWidget)
{
    ui->setupUi(this);
    exepath = QApplication::applicationDirPath();
}

WarehousingWidget::~WarehousingWidget()
{
    delete ui;
}

void WarehousingWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QImage t_image;
//    t_image.load(":/image/bg.png");
//    t_image = t_image.scaled(this->width(),this->height());
//    painter.drawPixmap(0,0,this->width(),this->height(),QPixmap::fromImage(t_image));

}

void WarehousingWidget::on_pushButton_img_clicked()
{
    QString t_fileName = QFileDialog::getOpenFileName(this,
                                                      tr("Open Image"), "/home/jana", tr("Image Files (*.png)"));
    QImage t_image;
    m_image.load(t_fileName);
    t_image = m_image.scaled(91,91);
    ui->label_dis->setPixmap(QPixmap::fromImage(t_image));

}

void WarehousingWidget::on_pushButton_1_clicked()
{
    QString lineEdit_a = ui->lineEdit_a->text();//获取界面输入
    QString lineEdit_b = ui->lineEdit_b->text();//获取界面输入
    QString lineEdit_c = ui->lineEdit_c->text();//获取界面输入
    QString lineEdit_d = ui->lineEdit_d->text();//
    QString lineEdit_e = ui->lineEdit_e->text();//
    QString lineEdit_f = ui->lineEdit_f->text();//
    QString lineEdit_g = ui->lineEdit_g->text();//
    if(lineEdit_a == "" || lineEdit_b == ""|| lineEdit_c == ""|| lineEdit_d == ""|| lineEdit_e == ""|| lineEdit_f == ""|| lineEdit_g == ""){//判断输入情况
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("输入不能为空");
        msgBox.exec();
    }else{
        QVector<QVector<QString>> t_vecall = DataBasePri::queryAll("products");
        bool exist = false;
        int numa = 0;
        for(int i = 0;i < t_vecall.size();i++){
            if(t_vecall.at(i).at(0)==lineEdit_a){
                numa = t_vecall.at(i).at(2).toInt();
                exist = true;
                break;
            }
        }

        if (! DataBasePri::database.open()){//打开数据库
            qCritical() << DataBasePri::database.lastError().text();
            return;
        }
        QSqlQuery query(DataBasePri::database);//创建数据库请求对象
        QString t_str;
        if(exist == false){
            t_str = QString::fromLocal8Bit("insert into products values('%1','%2','%3','%4','%5','%6','%7','%8')")
                    .arg(lineEdit_a).arg(lineEdit_b).arg(lineEdit_c).arg(lineEdit_d).arg(lineEdit_e).arg(lineEdit_f).arg(lineEdit_g).arg(lineEdit_a+".png");
        }else{
            numa+=lineEdit_c.toInt();
            t_str=QString::fromLocal8Bit("update user set num = '%1' where id = '%2'").arg(numa).arg(lineEdit_a);
        }
        bool ret = query.exec(t_str);
        qDebug()<<t_str;

        if(ret == true){
            QString ctime = QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss");
            m_image.save(exepath+"/image/"+lineEdit_a+".png");
            //入库记录
            QString t_str = QString::fromLocal8Bit("insert into warehousing values('%1','%2','%3','%4','%5','%6','%7','无')")
                    .arg(ctime).arg(lineEdit_a).arg(lineEdit_b).arg(lineEdit_c).arg(lineEdit_d).arg(lineEdit_e).arg(lineEdit_f).arg(lineEdit_g);
            query.exec(t_str);
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("入库成功");
            msgBox.exec();
        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("入库失败");
            msgBox.exec();
        }
        DataBasePri::database.close();
    }
}

void WarehousingWidget::on_pushButton_2_clicked()
{
    emit sendDisWidget("m_SelectWidget");
}
